package com.jiaai.cloud.chatroom.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.page.FrontPage;
import com.jiaai.cloud.base.util.RestResultGenerator;
import com.jiaai.cloud.chatroom.entity.TChatRoom;
import com.jiaai.cloud.chatroom.entity.TChatRoomSearch;
import com.jiaai.cloud.chatroom.service.ITChatRoomService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 聊天室 前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-08-01
 */
@RestController
@RequestMapping("/api/manage/tChatRoom")
public class TChatRoomController {
    @Autowired
    private ITChatRoomService itChatRoomService;


    //可以根据Id查找
    @RequestMapping(value = "/findByAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findByAll(TChatRoomSearch search) throws ServiceException {
        try {
            EntityWrapper wrapper=new EntityWrapper();
            if(!StringUtils.isEmpty(search.getTitle())){
                wrapper.eq("r.title",search.getTitle());
            }
            if(search.getRoomId()!=null){
                wrapper.eq("roomId",search.getRoomId());
            }
            if(!StringUtils.isEmpty(search.getUserNo())){
                wrapper.like("userNo",search.getUserNo());
            }
            if(!StringUtils.isEmpty(search.getName())){
                wrapper.like("name",search.getName());
            }
            wrapper.groupBy("roomId");
            return RestResultGenerator.genSuccessResult(itChatRoomService.findRoomFully(search.getPagePlus(),wrapper));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }
    //通过改anchorId改status状态
    @RequestMapping(value = "/changeStatus/{roomId}/{status}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object changeStatus(@PathVariable("roomId") Long roomId, @PathVariable("status") Integer status) throws ServiceException {
        try {
            TChatRoom tChatRoom = new TChatRoom();
            tChatRoom.setRoomId(roomId);
            tChatRoom.setStatus(status);
            itChatRoomService.updateById(tChatRoom);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e) {
            throw new ServiceException("修改失敗", e.getMessage());
        }
    }





}
